//
//  SideSlipView.swift
//  Wallpaper
//
//  Created by    on 2023/12/19.
//

import UIKit

class SideSlipView: UIView {
    
    
    override func awakeFromNib() {
        
    }
    
    static func show() {
        let side = SideSlipView.loadFromNib()
        currentWindow.addSubview(side)
        currentWindow.bringSubviewToFront(side)
        side.snp.makeConstraints { make in
            make.right.equalTo(currentWindow.snp.left).offset(0)
            make.top.equalTo(currentWindow.snp.top)
            make.width.equalTo(260)
            make.bottom.equalTo(currentWindow.snp.bottom)
        }
        
        let cancel = UIButton(type: .custom)
        cancel.backgroundColor = .clear
        currentWindow.addSubview(cancel)
        cancel.snp.makeConstraints { make in
            make.left.equalTo(side.snp.right)
            make.top.bottom.right.equalTo(0)
        }
        currentWindow.layoutIfNeeded() // 立即更新布局

        side.snp.updateConstraints { make in
            make.right.equalTo(currentWindow.snp.left).offset(260)
            make.top.equalTo(currentWindow.snp.top)
            make.width.equalTo(260)
            make.bottom.equalTo(currentWindow.snp.bottom)
        }
        
       
        
        cancel.addTarget(side, action: #selector(hide), for: .touchUpInside)
        cancel.tag = 9999
        UIView.animate(withDuration: 0.5) {
            currentWindow.layoutIfNeeded()
            cancel.alpha = 1
        } completion: { result in
            
        }

    }
    
    @objc func hide() {
        self.snp.updateConstraints { make in
            make.right.equalTo(currentWindow.snp.left).offset(0)
            make.top.equalTo(currentWindow.snp.top)
            make.width.equalTo(260)
            make.bottom.equalTo(currentWindow.snp.bottom)
        }
       let cancel = currentWindow.viewWithTag(9999) ?? UIView()
        UIView.animate(withDuration: 0.5) {
            currentWindow.layoutIfNeeded()
        } completion: { result in
            cancel.removeFromSuperview()
            self.removeFromSuperview()
        }
    }

}
